package com.macro.mall.portal.controller.oms;

import com.github.pagehelper.Page;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.model.UmsMember;
import com.macro.mall.oms.domain.OrderItem;
import com.macro.mall.oms.entity.OrderItemEntity;
import com.macro.mall.oms.service.order.OrderItemService;
import com.macro.mall.oms.service.order.bo.PagedQueryOrderItemInput;
import com.macro.mall.portal.controller.BaseController;
import com.macro.mall.portal.dto.oms.PagedQueryOrderItemReqDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 订单明细控制器
 *
 * @author xuyanjun
 * @date 2019-12-15
 */
@Api(tags = "OrderItemController", description = "订单明细")
@Slf4j(topic = "OrderItemController")
@RestController
@RequestMapping("/orderItem")
public class OrderItemController extends BaseController {

    @Autowired
    private OrderItemService orderItemService;

    /**
     * 分页查询订单明细
     *
     * @return 分页后的订单明细
     */
    @PreAuthorize("isAuthenticated()")
    @ApiOperation("分页查询订单明细")
    @PostMapping("pagedQuery")
    public CommonPage<OrderItem> pagedQuery(@RequestBody PagedQueryOrderItemReqDto reqDto) {
        PagedQueryOrderItemInput pagedQueryOrderItemInput = new PagedQueryOrderItemInput();
        pagedQueryOrderItemInput
                .setPageIndex(reqDto.getPageIndex())
                .setPageSize(reqDto.getPageSize());

        UmsMember currentUser = getCurrentUser();

        Page<OrderItemEntity> page = orderItemService.pagedQuery(pagedQueryOrderItemInput);

        List<OrderItem> orderItems = orderItemService.entityToDomain(page);

        return CommonPage.restPage(orderItems, page);
    }

    /**
     * 根据id获取订单明细
     *
     * @param id id
     * @return 订单明细
     */
    @ApiOperation("根据id获取订单明细")
    @GetMapping("{id}")
    public CommonResult<OrderItem> getById(@PathVariable long id) {
        if (id <= 0) {
            throw new BusinessException("要获取的订单明细Id不能为空。");
        }

        OrderItemEntity orderItemEntity = orderItemService.getById(id);
        if (orderItemEntity == null) {
            throw new BusinessException(String.format("要获取的订单明细id:%s不存在。", id));
        }

        OrderItem orderItem = orderItemService.entityToDomain(orderItemEntity);
        return CommonResult.success(orderItem);
    }

    /**
     * 根据批量id获取订单明细
     *
     * @param ids ids
     * @return 订单明细
     */
    @ApiOperation("根据批量id获取订单明细")
    @PostMapping("listByBatchIds")
    public CommonResult<List<OrderItem>> listByBatchIds(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("要获取的订单明细Id集合不能为空。");
        }

        List<OrderItemEntity> orderItemEntities = orderItemService.listByBatchIds(ids);

        List<OrderItem> orderItems = orderItemService.entityToDomain(orderItemEntities);
        return CommonResult.success(orderItems);
    }

}

